package com.china08.yxyapi.api.schwebsite.index;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.util.Assert;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import com.china08.yxyapi.api.YxyApiBaseController;
import com.china08.yxyapi.entity.db.sws.SwsSetting;
import com.china08.yxyapi.entity.db.sws.SwsSettingDomains;
import com.china08.yxyapi.model.YxyApiReqModel;
import com.china08.yxyapi.model.YxyApiRespModel;
import com.china08.yxyapi.repository.db.SwsSettingDomainsRepository;
import com.china08.yxyapi.repository.db.SwsSettingRepository;

@CrossOrigin
@RestController("SwsIndexSchoolLogoController")
public class SwsIndexSchoolLogoController extends YxyApiBaseController {

    @Autowired
    private SwsSettingRepository swsSettingRepository;

    @Autowired
    private SwsSettingDomainsRepository swsSettingDomainsRepository;

    @RequestMapping(value = "/api_yw/schwebsite/index/schoollogo", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public RespModel process(HttpServletRequest request, HttpServletResponse response,
            @RequestBody(required = false) ReqModel reqModel) {

        String domainName = getDomainName(request);

        Assert.isTrue(StringUtils.isNotBlank(domainName), "域名为空");
        // 通过域名获取学校ID
        SwsSettingDomains settingDomains = swsSettingDomainsRepository.findByDomainName(domainName);

        Assert.isTrue(null != settingDomains, "域名对象为空");

        String schoolId = settingDomains.getSchoolId();

        SwsSetting swsSetting = swsSettingRepository.findBySchoolId(schoolId);

        Assert.isTrue(null != swsSetting, "学校配置为空");
        // ##
        // 哈尔滨旧域名
        if (domainName.contains("hrbeduy")) {
            domainName = "//www.hrbeduyun.com";
        }
        // ##
        // 哈尔滨新域名
        if (domainName.contains("hrbeduy")) {
            domainName = "//www.hrbeduy.com";
        }
        if (domainName.contains("91yunxiao")) {
            domainName = "//www.91yunxiao.com";
        }
        if (domainName.contains("91eduy")) {
            domainName = "//www.91yunxiao.com";
        }
        RespModel respModel = new RespModel();
        String string = swsSetting.getLogoSetting().toString();
        if (string.equals("WEB_LOGO")) {
            if (StringUtils.isNotBlank(swsSetting.getSchoolLogo())) {
                String indexString = swsSetting.getSchoolLogo().substring(5, 6);
                if (indexString.equals(":")) {
                    respModel.schoolLogo = swsSetting.getSchoolLogo().substring(6, swsSetting.getSchoolLogo().length());
                } else {
                    respModel.schoolLogo = swsSetting.getSchoolLogo().substring(5, swsSetting.getSchoolLogo().length());
                }
            } else {
                respModel.schoolLogo = "";
            }
        } else {
            respModel.schoolLogo = swsSetting.getSchoolLogo();
        }
        respModel.fontFamily = swsSetting.getFontFamily();
        respModel.schoolName = swsSetting.getSchoolName();
        respModel.logoSetting = swsSetting.getLogoSetting().toString();
        if (StringUtils.isNotBlank(swsSetting.getSchoolBadge())) {
            String indexString = swsSetting.getSchoolBadge().substring(5, 6);
            if (indexString.equals(":")) {
                respModel.schoolBadge = swsSetting.getSchoolBadge().substring(6, swsSetting.getSchoolBadge().length());
            } else {
                respModel.schoolBadge = swsSetting.getSchoolBadge().substring(5, swsSetting.getSchoolBadge().length());
            }
        } else {
            respModel.schoolBadge = "";
        }
        respModel.fontColor = swsSetting.getFontColor();
        respModel.fontSize = swsSetting.getFontSize();
        respModel.domainName = domainName;
        respModel.schoolId = schoolId;
        return respModel;
    }

    protected static class ReqModel extends YxyApiReqModel {

    }

    protected static class RespModel extends YxyApiRespModel {

        public String schoolLogo;

        public String schoolName;
        // 学校名称字体
        public String fontFamily;

        public String logoSetting;

        public String schoolBadge;

        public String domainName;
        // 学校名称字号
        public Integer fontSize;
        // 学校名称颜色
        public String fontColor;
        // 学校ID
        public String schoolId;

    }
}
